import App from './App.vue'
import router from './router'
import store from './store'
import '@/assets/less/index.less'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import {createApp} from 'vue'
import axios from "axios";
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './assets/css/global.css'
import Global from "@/Global";
import Login from "@/components/Login";
import Register from "@/components/Register";
import MyList from "@/components/MyList";
import 'element-plus/theme-chalk/index.css'
import Personal from "@/views/Personal";
import ProductDetail from "@/views/ProductDetail";



const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

/* 预防ResizeObserver loop limit exceeded 报错 ------------------------------*/
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
      let context = this;
      let args = arguments;
      clearTimeout(timer);
      timer = setTimeout(function () {
        fn.apply(context, args);
      }, delay);
    }
  }
  
  const _ResizeObserver = window.ResizeObserver;
  window.ResizeObserver = class ResizeObserver extends _ResizeObserver{
    constructor(callback) {
      callback = debounce(callback, 16);
      super(callback);
    }
  }
/*------------------------------------------------------------------------- */  

app.use(Global)
app.use(ElementPlus)
app.config.globalProperties.$axios = axios;


// 全局组件
app.component(Login.name, Login);
app.component(Register.name, Register);
app.component(MyList.name, MyList);
app.component(ProductDetail.name,ProductDetail)

// 全局请求拦截器
// axios.interceptors.request.use(
//     config => {
//         return config;
//     },
//     error => {
//         // 跳转error页面
//         router.push({path: "/error"});
//         return Promise.reject(error);
//     })
//全局响应拦截器
// axios.interceptors.response.use(
//     res => {
//         if (res.data.code === "401") {
//             // 401表示没有登录
//             // 提示没有登录
//             app.config.globalProperties.notifyError(res.data.msg);
//             // 修改vuex的showLogin状态,显示登录组件
//             store.dispatch("setShowLogin", true);
//         }
//         if (res.data.code === "500") {
//             // 500表示服务器异常
//             // 跳转error页面
//             router.push({path: "/error"});
//             return res
//         }
//     },
//     error => {
//         // 跳转error页面
//         router.push({path: "/error"});
//         return Promise.reject(error);
//     })

app.use(store)
app.use(router).mount('#app')
